﻿using AntDesign;
using EFCore_Dbcontext.Context;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
using System.Linq.Expressions;
using System.Text.Json.Nodes;
namespace BlazorDemo.Data
{

    public class DbContexts : IZsg
    {
        public DbContexts(WeddingContext context, IMessageService message)
        {
            dB = context;
            _message = message;
        }
        private readonly WeddingContext dB;
        private readonly IMessageService _message;
        public async Task<string> Add<T>(int id, T data) where T : class
        {
            string strErr = string.Empty;
            try
            {
                // 将数据添加到数据库中
                var item = await dB.Set<T>().FindAsync(id);
                if (item == null)
                    dB.Set<T>().Add(data);
                // 保存数据库更改
                await dB.SaveChangesAsync();
                return strErr;
            }
            catch (Exception ex)
            {
                strErr = "数据添加失败：" + ex.Message;
                return strErr;
            }
        }

        public async Task<string> Del<T>(int id) where T : class
        {
            string strErr = string.Empty;
            try
            {
                // 从数据库中获取要删除的资源
                var item = await dB.Set<T>().FindAsync(id);
                if (item == null) strErr = "未能找到要删除的数据！";

                // 从数据库中删除资源
                dB.Set<T>().Remove(item);

                // 保存更改到数据库
                dB.SaveChanges();

                // 返回 true 表示删除成功

            }
            catch (Exception ex)
            {
                strErr = "未能找到要数据！" + ex.Message;
                throw new Exception("未能找到要数据！" + ex.Message);
            }
            return strErr;
        }

        public async Task<string> GetList<T>(int pageNumber, int pageSize) where T : class
        {
            try
            {
                IQueryable<T> query = dB.Set<T>();
                // 从数据库中获取要删除的资源
                var pagedData = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync();
                int Count = await query.CountAsync();
                return JsonConvert.SerializeObject(new
                {
                    Count = Count,
                    _Model = pagedData
                });
            }
            catch (Exception ex)
            {
                throw new Exception("未能找到要数据！" + ex.Message);
            }
        }
        public async Task<List<T>> GetList<T>(Expression<Func<T, bool>> predicate, int pageNumber, int pageSize) where T : class
        {
            try
            {
                IQueryable<T> query = dB.Set<T>().Where(predicate);
                int totalItems = await query.CountAsync(); // 总数据条数
                int totalPages = (int)Math.Ceiling((double)totalItems / pageSize); // 总页数
                var pagedData = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); // 分页后的数据
                return pagedData;
            }
            catch (Exception ex)
            {
                string errorMessage = "未能找到要数据！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
            }
        }
        public async Task<List<T>> GetList<T>(IQueryable<T> queryable) where T : class
        {
            try
            {
                IQueryable<T> query = dB.Set<T>();
                var pagedData = await queryable.ToListAsync();

                return pagedData;
            }
            catch (Exception ex)
            {
                string errorMessage = "未能找到要数据！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
            }
        }

        public async Task<string> Put<T>(int id, T data) where T : class
        {
            string strErr = string.Empty;
            try
            {
                var item = await dB.Set<T>().FindAsync(id);
                if (item == null) return strErr = "未能找到要修改的数据！"; ;
                dB.Entry(item).CurrentValues.SetValues(data);
                await dB.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string errorMessage = "修改失败！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
               
            }


            return strErr;
        }

        public async Task<T> GetOne<T>(int id) where T : class
        {

            try
            {
                var item = await dB.Set<T>().FindAsync(id);
                return item;
            }
            catch (Exception ex)
            {
                string errorMessage = "未能找到要数据！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
            }
        }

        public async Task<List<T>> GetList<T>(Expression<Func<T, bool>>? values = null, int pageNumber = 1, int pageSize = 8, int Count = 0) where T : class
        {
            try
            {
                IQueryable<T> query = dB.Set<T>().Where(values);
                var pagedData = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); // 分页后的数据
                return pagedData;
            }
            catch (Exception ex)
            {
                string errorMessage = "未能找到要数据！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
            }
        }



        public List<T> GetList<T>(ref int Count, int Skip = 1, int Task = 8) where T : class
        {
            IQueryable<T> query = dB.Set<T>();
            Count = query.Count();
            var pagedData = query.Skip((Skip - 1) * Task).Take(Task).ToList();
            return pagedData;
        }

        public async void ShowMess(string successMessage, EShowMess mess)
        {
            switch (mess)
            {
                case EShowMess.Error:
                    await _message.Error(successMessage);
                    break;
                case EShowMess.Success:
                    await _message.Success(successMessage);
                    break;
                case EShowMess.Warning:
                    await _message.Warning(successMessage);
                    break;
                case EShowMess.Info:
                    await _message.Info(successMessage);
                    break;
                case EShowMess.Loading:
                    await _message.Loading(successMessage);
                    break;
                default:
                    break;
            }

        }

        public async Task<List<T>> GetList<T>(IQueryable<T> predicate, int pageNumber = 1, int pageSize = 9999) where T : class
        {

            try
            {
                IQueryable<T> query = dB.Set<T>();
                query = predicate;
                var pagedData = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync(); // 分页后的数据
                return pagedData;
            }
            catch (Exception ex)
            {
                string errorMessage = "未能找到要数据！" + ex.Message;
                ShowMess(errorMessage, EShowMess.Info);
                throw new Exception(errorMessage);
            }

        }


    }
}
